package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.been.WorkItem;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuchen on 2016/9/20.
 */
public class WorkServiceObjDBBiz extends BaseSql {

    private String tableName = "WorkServiceObj";

    private String WorkType = "work_type";
    private String WorkId = "work_id";
    private String ObjectId = "object_id";
    private String ObjectName = "object_name";
    private String ObjectType = "object_type";
    private String ServiceobjId = "serviceobj_id";
    private String ScheduleId = "schedule_id";
    private String IsDone = "isdone";
    private String FormHasApply = "form_hasapply";
    private SQLiteDatabase sqldb;
    private MyDBHelper helpDb;
    //存放userId
    private String Remark1 = "remark1";
    private String Remark2 = "remark2";
    private String Remark3 = "remark3";

    protected WorkServiceObjDBBiz() {
        super();
    }

    protected WorkServiceObjDBBiz(MyDBHelper db) {
        super(db);
        helpDb = db;
    }

    @Override
    public void create(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + tableName + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                WorkType + "  text," +
                WorkId + " text," +
                ObjectId + " text," +
                ObjectName + " text," +
                ObjectType + " text ," +
                ServiceobjId + " text ," +
                ScheduleId + " text," +
                IsDone + " text," +
                FormHasApply + " text," +
                Remark1 + " text ," +
                Remark2 + " text ," +
                Remark3 + " text );");
    }

    @Override
    public void upgrade(SQLiteDatabase db) {

    }

    protected void checkDb() {
        if (sqldb == null || !sqldb.isOpen()) {
            sqldb = helpDb.getWritableDatabase();
        }
    }

    private static final String TAG = "WorkServiceObjDBBiz";

    /**
     * 根据workType workId查询对应记录
     *
     * @param workType
     * @param workId
     * @return
     */
    public List<WorkItem> queryRowsByWorkType(String workType, String workId, String userId) {
        LogUtil.d(TAG, "queryRowsByWorkType workType " + workType + " workId" + workId + " userId " + userId);
        checkDb();
        List<WorkItem> itemList = new ArrayList<WorkItem>();
        StringBuilder selectSb = new StringBuilder();
        String[] selectionArgs;
//        if (!TextUtils.isEmpty(workType) && !TextUtils.isEmpty(workId)) {
        selectSb.append(WorkType + "= ? and  " + WorkId + "= ? and " + Remark1 + " = ?");
        selectionArgs = new String[]{workType, workId, userId};
//        } else if (!TextUtils.isEmpty(workType)) {
//            selectSb.append(WorkType + "= ?  ");
//            selectionArgs = new String[]{workType};
//        } else if (!TextUtils.isEmpty(workId)) {
//            selectSb.append(WorkId + "= ? ");
//            selectionArgs = new String[]{workId};
//        } else {
//            return itemList;
//        }
        String selection = selectSb.toString();
        String[] columns = {WorkType, WorkId, ObjectId, ObjectName, ObjectType, ServiceobjId, ScheduleId, IsDone,
                FormHasApply};
        Cursor cur = sqldb.query(tableName, columns, selection, selectionArgs, null, null, null);
        if (cur != null) {
            while (cur.moveToNext()) {
                String objId = cur.getString(2);
                String objName = cur.getString(3);
                String objType = cur.getString(4);
                String serviceObJId = cur.getString(5);
                String scheduleId = cur.getString(6);
                String isDone = cur.getString(7);
                String formHasApply = cur.getString(8);
                LogUtil.v(TAG, "queryRowsByWorkType workType:" + workType + " objName:" + objName);
                WorkItem wi = new WorkItem();
                wi.work_type = workType;
                wi.work_id = workId;
                wi.object_id = objId;
                wi.object_name = objName;
                wi.object_type = objType;
                wi.serviceobj_id = serviceObJId;
                wi.schedule_id = scheduleId;
                wi.isdone = isDone;
                wi.form_hasapply = formHasApply;
                itemList.add(wi);
            }
            cur.close();
        }
        return itemList;
    }

    public void updateAllRows(String workType, String workId, String userId, List<WorkItem> dataList) {
        //清除之前的数据，再次插入新的数据
        checkDb();
        selectionArgs = new String[]{workType, workId, userId};
        selection = WorkType + " = ?  and " + WorkId + " = ? and " + Remark1 + " = ?";
        int deleteResult = sqldb.delete(tableName, selection, selectionArgs);
        LogUtil.d(TAG, "deleteResult " + deleteResult);
        values = new ContentValues();
        for (WorkItem workItem : dataList) {
            values.put(WorkType, workType);
            values.put(WorkId, workId);
            values.put(ObjectId, workItem.object_id);
            values.put(ObjectName, workItem.object_name);
            values.put(ObjectType, workItem.object_type);
            values.put(ServiceobjId, workItem.serviceobj_id);
            values.put(ScheduleId, workItem.schedule_id);
            values.put(IsDone, workItem.isdone);
            values.put(FormHasApply, workItem.form_hasapply);
            values.put(Remark1, userId);
            sqldb.insert(tableName, null, values);
        }
    }

    @Override
    protected synchronized int saveOrUpdate(String tableName) {
        return super.saveOrUpdate(tableName);
    }

    @Override
    protected synchronized int update(String tableName) {
        return super.update(tableName);
    }

    @Override
    protected int delete(String tableName) {
        return super.delete(tableName);
    }

    @Override
    protected int total(String table) {
        return super.total(table);
    }
}
